import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;

public class ImageInJFrame extends JFrame {
    static Connection theConnection;
    static LinkedList<String> filmImagesList;
    static ArrayList<JButton> buttons = new ArrayList() ;
    static ArrayList<String> filmHyperlinks = new ArrayList();
    static boolean loggedIn = false;
    static String loggedInName = "";
    JButton blogin = new JButton("Member Login");
    JButton bAdminLogin = new JButton("Administrator Login");
    JLabel loggedInUserName = new JLabel();
    BrowserButton panel;


    public static void main(String args[]){
        try{
            theConnection = DBConnection.makeConnection();
            filmImagesList = DBConnection.getLLQuery( "select filmposter from film", theConnection );
        }catch (Exception e){
        }
        new ImageInJFrame().start();
    }

    public void start(){
        Image bGround = ReadImage.getImage("http://wallpapers.wallbase.cc/rozne/wallpaper-389121.jpg");
        Browser bGPanel = new Browser( bGround );
        add(bGPanel);

        int xPos = 20;
        int yPos = 50;

        for( String link : filmImagesList ){
            Image filmImage = ReadImage.getImage( link );
            panel = new BrowserButton( filmImage );
            buttons.add( panel );
            filmHyperlinks.add(link);
            panel.setBounds(xPos, yPos, 130, 200);
            bGPanel.add(panel);
            xPos = xPos +250;
        }

        for(int i=0; i< buttons.size(); i++){
            buttons.get(i).setActionCommand( filmHyperlinks.get(i) );
            buttons.get(i).addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    String hyperlink = e.getActionCommand();
                    String filmDetails = Film.getFilm( hyperlink );
                    FilmDetailsFrame filmInfo = new FilmDetailsFrame(hyperlink, filmDetails);
                }
            });
        }
        loggedInUserName.setBounds(100,10,150,20);
        blogin.setBounds(1250,10,150,20);
        bAdminLogin.setBounds(1050,10,175,20);
        bGPanel.add(blogin);
        bGPanel.add(bAdminLogin);

        setVisible(true);
        setSize(1440,850);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        clickLogin();
        clickAdminLogin();
    }

    public void clickLogin(){
        blogin.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ){
                Login loginFrame = new Login();
                loginFrame.setVisible( true );
            }
        });
    }

    public void clickAdminLogin(){
        bAdminLogin.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ){
                AdminLogin loginFrame = new AdminLogin();
                loginFrame.setVisible( true );
            }
        });
    }
}

